<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="企业ID" prop="coCode">
      <el-input v-model="dataForm.coCode" placeholder="企业ID"></el-input>
    </el-form-item>
    <el-form-item label="企业名称" prop="coDesc">
      <el-input v-model="dataForm.coDesc" placeholder="企业名称"></el-input>
    </el-form-item>
    <el-form-item label="省" prop="coProvince">
      <el-input v-model="dataForm.coProvince" placeholder="省"></el-input>
    </el-form-item>
    <el-form-item label="市" prop="coCity">
      <el-input v-model="dataForm.coCity" placeholder="市"></el-input>
    </el-form-item>
    <el-form-item label="详细地址" prop="coAddress">
      <el-input v-model="dataForm.coAddress" placeholder="详细地址"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="coPhone">
      <el-input v-model="dataForm.coPhone" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="主要联系人" prop="coPerson">
      <el-input v-model="dataForm.coPerson" placeholder="主要联系人"></el-input>
    </el-form-item>
    <el-form-item label="邮件地址" prop="coEmail">
      <el-input v-model="dataForm.coEmail" placeholder="邮件地址"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          coId: 0,
          coCode: '',
          coDesc: '',
          coProvince: '',
          coCity: '',
          coAddress: '',
          coPhone: '',
          coPerson: '',
          coEmail: ''
        },
        dataRule: {
          coCode: [
            { required: true, message: '企业ID不能为空', trigger: 'blur' }
          ],
          coDesc: [
            { required: true, message: '企业名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.coId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.coId) {
            this.$http({
              url: this.$http.adornUrl(`/udp/r5rmcompany/info/${this.dataForm.coId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.coCode = data.r5rmCompany.coCode
                this.dataForm.coDesc = data.r5rmCompany.coDesc
                this.dataForm.coProvince = data.r5rmCompany.coProvince
                this.dataForm.coCity = data.r5rmCompany.coCity
                this.dataForm.coAddress = data.r5rmCompany.coAddress
                this.dataForm.coPhone = data.r5rmCompany.coPhone
                this.dataForm.coPerson = data.r5rmCompany.coPerson
                this.dataForm.coEmail = data.r5rmCompany.coEmail
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/udp/r5rmcompany/${!this.dataForm.coId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'coId': this.dataForm.coId || undefined,
                'coCode': this.dataForm.coCode,
                'coDesc': this.dataForm.coDesc,
                'coProvince': this.dataForm.coProvince,
                'coCity': this.dataForm.coCity,
                'coAddress': this.dataForm.coAddress,
                'coPhone': this.dataForm.coPhone,
                'coPerson': this.dataForm.coPerson,
                'coEmail': this.dataForm.coEmail
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
